package dsek.hugo;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class main extends Activity {
	
	private EditText edit;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        edit = (EditText)findViewById(R.id.entry);
    }
    
    public void printHelp(View v) {
    	Toast.makeText(this, R.string.help_string, Toast.LENGTH_LONG).show();
    }
    
    public void processString(View v) {
    	String tempStr = edit.getText().toString();
		String[] text = tempStr.split("\n");
		edit.setText("");
		
		
		for(int i = 0; i < text.length; i++) {
			if (text[i].length() < 4) {
				Toast.makeText(this, "Message: " + text[i] + "\nis to short", Toast.LENGTH_SHORT).show();
			} else if (text[i].length() >= 140) {
				Toast.makeText(this, "Message is longer than 140 chars", Toast.LENGTH_SHORT).show();
			} else {
				telnetHugo(text[i]);
			}
		}
    }
    
    private void telnetHugo(String string) {
    	
		try {
			Socket connection = new Socket("hugo.dsek.lth.se", 1101); 
			PrintStream out = new PrintStream(connection.getOutputStream());
			out.println(string);
			out.close();
			connection.close();
			Toast.makeText(this, "Message " + string + " has been sent!", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(this, "Could not connect to Hugo. Message " + string + " NOT sent.", Toast.LENGTH_LONG).show();
		}
    }
}